home *** CD-ROM | disk | FTP | other *** search
- Converting Decimals to Fractions
- (BYTE Magazine May 1985 by Dan Sandberg)
-
- If you needed the solution of 11/17 + 13/19 - 139/323 - 37/15 +
- 47/21 in fractional form, finding the lowest common denominator might
- be difficult. Instead, you can plug the decimal equivalent,
- 0.672357364 into the following program and obtain a solution of
- 7601/11305.
- The program can help you factor 133133/1101373 to 11/91 or
- verify that sine 60 degrees equals the square root of 3 divided by 2.
- Program one, which returns a fraction for every decimal input,
- uses a short algorithm. First, the program inverts the decimal to
- obtain a number greater than 1. The routine saves the integer and
- again inverts the decimal remainder. So it continues, until the
- algorithm finds a denominator that supports an integer numerator.
- If you want fractions printed in mixed form, add:
-
- 35 IF INT(A)>0 THEN PRINT INT(A);"+";C*(A-INT(A));'/';C
-
- Program One:
-
- 10 INPUT A:B=0:C=1:D=ABS(A-INT(A))
- 20 IF D=0 THEN 40
- 30 E=1/D:F=C:C=INT(E)*C+B:B=F:D=E-INT(E):IF A*C<>INT(A*C) THEN 30
- 40 PRINT A*C;"/";C:GOTO 10
-
- Program two is shorter and faster, but it may require higher
- calculating precision. It never returns an inexact (even if close)
- fraction; it returns an error if you input insufficient precision.
- The program inverts the incoming number with a special algorithm until
- it finds the denominator. If the fraction is too difficult (i.e.,
- requiring greater precision), an overflow error will occur. If you
- enter too few decimal digits, the program, which does not round A*B
- to an integer, will warn you by writing a decimal numerator that is
- close to an integer. With a precision of 12 digits, 0.333333333333
- will generate the answer 1/3. However, 0.3333333333 will yield
- 0.9999999999/3. On the other hand, 0.333 gives the answer 333/1000,
- a useful feature for those needing precise fractions. Others might
- round A*B to the nearest integer. The constant, 0.00001, in line
- 110 is suitable for 12-digit calculating precision. Try constants
- like 0.0001 and 0,000001 to produce the best possible conversion
- capability. For mixed output, you can add:
-
- 130 PRINT INT(A);"+";(A-INT(A))*B;"/";B
-
- Program Two:
-
- 100 INPUT A:C=ABS(A):B=1
- 110 B=B/C:C=(1/C)-INT(1/C):IF C>0.00001 THEN 110
- 120 B=INT(B):PRINT A*B;"/";B:GOTO 100
-
- Program three detects constants like pi, the square root of 2,
- and square root of 3; enter the sine 60 degrees (0.8660254) and get
- the square root of 3 divided by 2. The program divides and multiplies
- the incoming decimals by the constants, one at a time, and uses a
- slightly modified version of program two as a subroutine to determine
- whether the constants form part of the fraction. The decimal equivalent
- of arctan(-1), -0.7853983, gives the answer -pi/4. Arcsin -1 returns
- -pi/2. You need not struggle with tables.
- Note that the program always places the square root in the
- numerator. Therefore, 1 divided by the square root of 3 will appear
- as SQR 3/3. To eliminate this, simply add:
-
- 125 B=INT(B):IF SQR(B)=H/A THEN L$=K$:K$="":A=A*B:B=1
-
- Program Three:
-
- 10 K$="":L$="":INPUT H:A=H:GOSUB 100
- 20 K$="sqr 2":A=H/SQR(2):GOSUB 100
- 30 K$="sqr 3":A=H/SQR(3):GOSUB 100
- 40 K$="sqr 5":A=H/SQR(5):GOSUB 100
- 45 PI=3.141592653589793#
- 50 K$="PI":A=H/PI:GOSUB 100
- 60 K$="PI exp 2":A=H/PI/PI:GOSUB 100
- 70 K$="":L$="PI":A=H*PI*PI:GOSUB 100
- 100 C=ABS(A):B=1
- 110 B=BC:C=(1/C)-INT(1/C):IF B>10000 THEN RETURN
- 120 IF C>0.00001 THEN 110
- 130 B=INT(B):PRINT A*B;K$;"/";B;L$:END
-
- -----------------------------------------------------------------
- Turbo Primes
- (PC World August 1986 Star-Dot-Star)
-
- PRIMES.BAS (PC World Nov 1985 *.*) ran faster than the routine
- used in the PC AT speed trials in "AT: The PC's Powerful Partner"
- (PC World Vol 2 No 13). PRIMES2.BAS is faster still. It finds all
- primes less than 1000 in just 14 seconds. For numbers above 1000,
- the speed difference is even more pronounced.
- PRIMES2.BAS maintains an array of markers, one for each number
- in the range to be calculated. Once each number is evaluated, the
- program steps through the array by multiples of that number and marks
- each of those array positions with a 1. Next, the program increases
- the value to be tested by 1 and checks the array to see if that value's
- position has been marked. If it has not, the value is displayed on
- screen, and its multiples in the array are marked. This process
- continues until all values in the range have been tested. Note that
- PRIMES2.BAS will run even faster if you delete line 150.
-
- 10 'PRIMES2.BAS
- 100 CLS:KEY OFF:DEFINT A-Z
- 110 INPUT "Primes up to: ",N
- 120 CLS:DIM M(N):T1$=TIME$:PU$=STRING$(LEN(STR$(N)),"#")
- 130 FOR C=2 TO N
- 140 IF M(C) THEN 190
- 150 PRINT USING PU$;C;
- 160 FOR L=C*2 TO N STEP C
- 170 M(L)=1
- 180 NEXT
- 190 NEXT
- 200 T2$=TIME$:PRINT:PRINT
- 210 T1=3600*VAL(MID$(T1$,1,2))+60*VAL(MID$(T1$,4,2))+VAL(MID$(T1$,7,2))
- 220 T2=3600*VAL(MID$(T2$,1,2))+60*VAL(MID$(T2$,4,2))+VAL(MID$(T2$,7,2))
- 230 PRINT "Calculation took";T2-T1;"seconds."
- 240 END
-